<?php

namespace api\modules\v2\controllers;


use api\modules\v2\resources\search\ArticleSearch;
use backend\models\search\ProductSearch;
use common\models\Article;

use yii\data\ActiveDataFilter;
use yii\data\ActiveDataProvider;
use yii\rest\ActiveController;

use yii\rest\Controller;
class ProductController extends Controller
{
    /**
     * @var string
     */
    //http://api.zuxianzhi.com/v1/article-category/index?filter[title]=yii2
    // expand=test&fields=type_id&sort=-update_at&filter[title]=yii2
    public $serializer = [
        'class' => 'yii\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function actionIndex()
    {
        $filter = new ActiveDataFilter([
            'searchModel' => 'backend\models\search\ProductSearch',
        ]);

        $filterCondition = null;

        if ($filter->load(\Yii::$app->request->get())) {
            $filterCondition = $filter->build();
            if ($filterCondition === false) {
                return $filter;
            }
        }
        $searchParams = [];
        if (!empty($filterCondition)) {
            foreach ($filterCondition as $key => $value) {
                if(is_array($value)){
                    $searchParams['ArticleSearch'][key($value)] = $value[key($value)];
                }else{
                    $searchParams['ArticleSearch'][$key] = $value;
                }
            }
        }


        $searchModel  = new ProductSearch();
        $dataProvider = $searchModel->search($searchParams);
        return $dataProvider;
    }
}
